<template>
    <Card>
		<Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
			<FormItem label="API key" prop="apikey">
				<Input v-model="formValidate.apikey" type="password" placeholder="请输入交易网站apikey"></Input>
			</FormItem>
			<FormItem label="API secret" prop="apikey">
				<Input v-model="formValidate.apisecret" type="password" placeholder="请输入交易网站apisecret"></Input>
			</FormItem>
			<FormItem label="买入费率" prop="mrfl">
				 <InputNumber :min="0" v-model="formValidate.mrfl"></InputNumber>%
			</FormItem>
			<FormItem label="卖出费率" prop="mcfl">
				<InputNumber :min="0" v-model="formValidate.mcfl"></InputNumber>%
			</FormItem>
			<FormItem>
				<Button type="primary" @click="handleSubmit('formValidate')">保存配置</Button>
				<Button type="primary" @click="handleTest('formValidate')">测试API连接</Button>
				<Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
			</FormItem>
		</Form>
	</Card>
</template>
<script>
import { ipcRenderer } from "electron";
var gate = require('../../gate/gate');
export default {
	data(){
		return {
			actiontype:'save',
			formValidate: {
				apikey: '',
				apisecret: '',
				mrfl:0,
				mcfl:0
			},
			ruleValidate: {
				apikey: [
					{ required: true, message: 'apikey不可为空', trigger: 'blur' }
				],
				apisecret: [
					{ required: true, message: 'apisecret不可为空', trigger: 'blur' }
				],
			}
		}
	},
	created(){
	
		ipcRenderer.on("getApisetting", (event, message) => {
			this.formValidate = message;
		});
		this.getMessage("getApisetting", this.$router.history.current.params.id);
		ipcRenderer.on("setApi", (event, message) => {
			if(message==='ok' && this.actiontype == 'save'){
				this.$Message.success('保存配置成功');
			}
			if(message==='ok' && this.actiontype == 'test'){
				this.$Message.success('测试连接成功');
			}
		});
	},
	methods:{
		getMessage: function(event, type) {
		  	ipcRenderer.send(event, type);
		},
		handleSubmit (name) {
			this.actiontype = 'save';
			this.$refs[name].validate((valid) => {
				if (valid) {
					let seddata = this.formValidate;
					seddata.id = this.$router.history.current.params.id;
					this.getMessage('setApi',seddata);
				} else {
					this.$Message.error('请填写正确信息');
				}
			})
		},
		handleTest (name){
			this.actiontype = 'test';
			this.$refs[name].validate((valid) => {
				//console.log(this.$store.state.apisettingname);
				if (valid) {
					let seddata = this.formValidate;
					seddata.id = this.$router.history.current.params.id;
					console.log(seddata)
					//this.getMessage('setApi',seddata);
					gate.getBalances(formValidate.apikey,formValidate.apisecret,function (res) {
						console.log(res);
					});
				} else {
					this.$Message.error('请填写正确信息');
				}
			})
		},
		handleReset (name) {
			this.$refs[name].resetFields();
		}
	},
	destroyed() {
		ipcRenderer.removeAllListeners("getApisetting");
		ipcRenderer.removeAllListeners("setApi");
	}
}
</script>